package org.dromara.business.api;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.business.domain.vo.TransactionInfoVO;
import org.dromara.business.service.BlockchainService;
import org.dromara.common.core.domain.R;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/blockchain")
@RequiredArgsConstructor
public class BlockchainApi {

    private final BlockchainService blockchainService;

    @GetMapping("/tx/{txHash}")
    public R<TransactionInfoVO> getTransaction(@PathVariable String txHash) {
        log.info("查询交易: {}", txHash);
        TransactionInfoVO transaction = blockchainService.getTransactionByHash(txHash);
        return R.ok(transaction);
    }

    @GetMapping("/tx/{txHash}/detail")
    public R<TransactionInfoVO> getTransactionDetail(@PathVariable String txHash) {
        log.info("查询交易详情: {}", txHash);
        TransactionInfoVO transaction = blockchainService.getTransactionWithReceipt(txHash);
        return R.ok(transaction);
    }

//    @PostMapping("/tx/save")
//    public R<BlockchainTransaction> saveTransaction(@Valid @RequestBody TransactionRequest request) {
//        log.info("保存交易记录: {}", request.getTxHash());
//        BlockchainTransaction transaction = blockchainService.saveTransaction(request);
//        return R.ok(transaction);
//    }
//
//    @GetMapping("/user/{userId}/transactions")
//    public R<List<BlockchainTransaction>> getUserTransactions(@PathVariable Long userId) {
//        log.info("查询用户{}的交易记录", userId);
//        List<BlockchainTransaction> transactions = blockchainService.getUserTransactions(userId);
//        return R.ok(transactions);
//    }
//
//    @GetMapping("/staking/{stakingId}/transactions")
//    public R<List<BlockchainTransaction>> getStakingTransactions(@PathVariable Long stakingId) {
//        log.info("查询质押订单{}的交易记录", stakingId);
//        List<BlockchainTransaction> transactions = blockchainService.getStakingTransactions(stakingId);
//        return R.ok(transactions);
//    }

    @GetMapping("/balance/{address}")
    public R<BigDecimal> getBalance(@PathVariable String address) {
        log.info("查询地址余额: {}", address);
        BigDecimal balance = blockchainService.getEthBalance(address);
        return R.ok(balance);
    }

    @GetMapping("/block/number")
    public R<Long> getBlockNumber() {
        Long blockNumber = blockchainService.getBlockNumber();
        return R.ok(blockNumber);
    }

    @GetMapping("/network/info")
    public R<String> getNetworkInfo() {
        String networkInfo = blockchainService.getNetworkInfo();
        return R.ok(networkInfo);
    }

    @GetMapping("/address/{address}/isContract")
    public R<Boolean> isContractAddress(@PathVariable String address) {
        log.info("检查是否为合约地址: {}", address);
        Boolean isContract = blockchainService.isContractAddress(address);
        return R.ok(isContract);
    }

    @GetMapping("/tx/{txHash}/confirmations")
    public R<Integer> getTransactionConfirmations(@PathVariable String txHash) {
        log.info("查询交易确认数: {}", txHash);
        Integer confirmations = blockchainService.getTransactionConfirmations(txHash);
        return R.ok(confirmations);
    }
}
